package com.fieldchain.ex.fa100.mvp.model;

import android.graphics.Bitmap;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.bean.User;
import com.fieldchain.ex.fa100.inter.ImageCallBack;
import com.fieldchain.ex.fa100.inter.IntegerCallBack;
import com.fieldchain.ex.fa100.mvp.base.MyBaseModel;
import java.util.HashMap;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.BeanRequest;
import com.fieldchain.ex.fa100.utils.net.modle.ImageRequest;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.fieldchain.ex.fa100.utils.share.ShareManager;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/3
 */
public class LoginModle extends MyBaseModel {

    public void login(String userName, String password, String imgCode,final IntegerCallBack integerCallBack){
        if(userName.length()==0){
            MyToast.show("请输入手机或邮箱账号");
            return;
        }
        int length=password.length();
        if(!(length>=6&&length<=20)){
            MyToast.show("密码格式为6-20位数字字母字符组合");
            return;
        }
        HashMap map=new HashMap();
        map.put("userName",userName);
        map.put("password",password);
        map.put("imgCode",imgCode);
        map.put("deviceId",ShareManager.getInstance().getDeviceId());
        HttpRequest.getInstance().postRequest(map, Constant.login, new BeanRequest<User>() {
            @Override
            public void onSucess(User user, int code, String msg) {
                if(code==Constant.netOk){
                    ShareManager.getInstance().saveUser(user);
                }
                integerCallBack.index(code);
                MyToast.show(msg);
            }

            @Override
            public void onFaild(Exception e) {
                integerCallBack.index(505);
            }
        });
    }

    public void login(String userName, String smsCode, final IntegerCallBack integerCallBack){
        if(smsCode.length()==0){
            MyToast.show("请输入手机验证码");
            return;
        }

        if(userName.length()==0){
            MyToast.show("请输入手机或邮箱账号");
            return;
        }
        HashMap map=new HashMap();
        map.put("userName",userName);
        map.put("smsCode",smsCode);
        map.put("deviceId",ShareManager.getInstance().getDeviceId());
        HttpRequest.getInstance().postRequest(map, Constant.login_sms, new BeanRequest<User>() {
            @Override
            public void onSucess(User user, int code, String msg) {
                if(code==Constant.netOk){
                    ShareManager.getInstance().saveUser(user);
                }
                integerCallBack.index(code);
                MyToast.show(msg);
            }

            @Override
            public void onFaild(Exception e) {
                integerCallBack.index(505);
            }
        });
    }
    public void getVer(final ImageCallBack callBack){
        HttpRequest.getInstance().getImage(Constant.image, new ImageRequest() {
            @Override
            public void imgReceive(Bitmap bit) {
                callBack.imageReceive(bit);
            }
        });
    }
    @Override
    public void detachView() {

    }
}
